 package server;

 import client.inventory.Item;
 import client.inventory.ItemLoader;
 import client.inventory.MapleInventoryType;
 import com.alibaba.druid.pool.DruidPooledConnection;
 import constants.ItemConstants;
 import database.DatabaseConnection;
 import java.io.Serializable;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import tools.Pair;



 public class MTSCart
   implements Serializable
 {
   private static final long serialVersionUID = 231541893513373578L;
   private final int characterId;
/*  25 */   private final List<Item> transfer = new ArrayList<>();
/*  26 */   private final List<Integer> cart = new ArrayList<>();
/*  27 */   private final List<Integer> notYetSold = new ArrayList<>(10);
/*  28 */   private int tab = 1;
/*  29 */   private int type = 0;
/*  30 */   private int page = 0;
/*  31 */   private List<Integer> currentViewingItems = new ArrayList<>();
/*  32 */   private int owedNX = 0;

   public MTSCart(int characterId) throws SQLException {
/*  35 */     this.characterId = characterId;
/*  36 */     for (Pair<Item, MapleInventoryType> item : (Iterable<Pair<Item, MapleInventoryType>>)ItemLoader.MTS_TRANSFER.loadItems(false, characterId).values()) {
/*  37 */       this.transfer.add(item.getLeft());
     }
/*  39 */     loadCart();
/*  40 */     loadNotYetSold();
   }

   public List<Item> getInventory() {
/*  44 */     return this.transfer;
   }

   public void addToInventory(Item item) {
/*  48 */     this.transfer.add(item);
   }

   public void removeFromInventory(Item item) {
/*  52 */     this.transfer.remove(item);
   }

   public List<Integer> getCart() {
/*  56 */     return this.cart;
   }

   public boolean addToCart(int car) {
/*  60 */     if (!this.cart.contains(Integer.valueOf(car))) {
/*  61 */       this.cart.add(Integer.valueOf(car));
/*  62 */       return true;
     }
/*  64 */     return false;
   }

   public void removeFromCart(int car) {
/*  68 */     for (int i = 0; i < this.cart.size(); i++) {
/*  69 */       if (((Integer)this.cart.get(i)).intValue() == car) {
/*  70 */         this.cart.remove(i);
       }
     }
   }

   public List<Integer> getNotYetSold() {
/*  76 */     return this.notYetSold;
   }

   public void addToNotYetSold(int car) {
/*  80 */     this.notYetSold.add(Integer.valueOf(car));
   }

   public void removeFromNotYetSold(int car) {
/*  84 */     for (int i = 0; i < this.notYetSold.size(); i++) {
/*  85 */       if (((Integer)this.notYetSold.get(i)).intValue() == car) {
/*  86 */         this.notYetSold.remove(i);
       }
     }
   }

   public int getSetOwedNX() {
/*  92 */     int on = this.owedNX;
/*  93 */     this.owedNX = 0;
/*  94 */     return on;
   }

   public void increaseOwedNX(int newNX) {
/*  98 */     this.owedNX += newNX;
   }

   public void save() throws SQLException {
/* 102 */     List<Pair<Item, MapleInventoryType>> itemsWithType = new ArrayList<>();

/* 104 */     for (Item item : getInventory()) {
/* 105 */       itemsWithType.add(new Pair(item, ItemConstants.getInventoryType(item.getItemId())));
     }

/* 108 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 109 */       ItemLoader.MTS_TRANSFER.saveItems(con, itemsWithType, this.characterId);
/* 110 */       try (PreparedStatement ps = con.prepareStatement("DELETE FROM mts_cart WHERE characterid = ?")) {
/* 111 */         ps.setInt(1, this.characterId);
/* 112 */         ps.execute();
       }
/* 114 */       try (PreparedStatement ps = con.prepareStatement("INSERT INTO mts_cart VALUES(DEFAULT, ?, ?)")) {
/* 115 */         ps.setInt(1, this.characterId);
/* 116 */         for (Iterator<Integer> iterator = this.cart.iterator(); iterator.hasNext(); ) { int i = ((Integer)iterator.next()).intValue();
/* 117 */           ps.setInt(2, i);
/* 118 */           ps.executeUpdate(); }

/* 120 */         if (this.owedNX > 0) {
/* 121 */           ps.setInt(2, -this.owedNX);
/* 122 */           ps.executeUpdate();
         }
       }
     }
   }


   public final void loadCart() throws SQLException {
/* 130 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 131 */       PreparedStatement ps = con.prepareStatement("SELECT * FROM mts_cart WHERE characterid = ?");
/* 132 */       ps.setInt(1, this.characterId);
/* 133 */       ResultSet rs = ps.executeQuery();

/* 135 */       while (rs.next()) {
/* 136 */         int iId = rs.getInt("itemid");
/* 137 */         if (iId < 0) {
/* 138 */           this.owedNX -= iId; continue;
/* 139 */         }  if (MTSStorage.getInstance().check(iId)) {
/* 140 */           this.cart.add(Integer.valueOf(iId));
         }
       }
/* 143 */       rs.close();
/* 144 */       ps.close();
     }
   }

   public final void loadNotYetSold() throws SQLException {
/* 149 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 150 */       PreparedStatement ps = con.prepareStatement("SELECT * FROM mts_items WHERE characterid = ?");
/* 151 */       ps.setInt(1, this.characterId);
/* 152 */       ResultSet rs = ps.executeQuery();

/* 154 */       while (rs.next()) {
/* 155 */         int pId = rs.getInt("id");
/* 156 */         if (MTSStorage.getInstance().check(pId)) {
/* 157 */           this.notYetSold.add(Integer.valueOf(pId));
         }
       }
/* 160 */       rs.close();
/* 161 */       ps.close();
     }
   }

   public void changeInfo(int tab, int type, int page) {
/* 166 */     if (tab != this.tab || type != this.type) {
/* 167 */       refreshCurrentView(tab, type);
     }
/* 169 */     this.tab = tab;
/* 170 */     this.type = type;
/* 171 */     this.page = page;
   }

   public int getTab() {
/* 175 */     return this.tab;
   }

   public int getType() {
/* 179 */     return this.type;
   }

   public int getPage() {
/* 183 */     return this.page;
   }

   public List<Integer> getCurrentViewPage() {
/* 187 */     List<Integer> ret = new ArrayList<>();
/* 188 */     int size = this.currentViewingItems.size() / 16 + ((this.currentViewingItems.size() % 16 > 0) ? 1 : 0);
/* 189 */     if (this.page > size) {
/* 190 */       this.page = 0;
     }
/* 192 */     for (int i = this.page * 16; i < this.page * 16 + 16 && 
/* 193 */       this.currentViewingItems.size() > i; i++) {
/* 194 */       ret.add(this.currentViewingItems.get(i));
     }



/* 199 */     return ret;
   }

   public List<Integer> getCurrentView() {
/* 203 */     return this.currentViewingItems;
   }

   public void refreshCurrentView() {
/* 207 */     refreshCurrentView(this.tab, this.type);
   }

   public void refreshCurrentView(int newTab, int newType) {
/* 211 */     this.currentViewingItems.clear();
/* 212 */     if (newTab == 1) {
/* 213 */       this.currentViewingItems = MTSStorage.getInstance().getBuyNow(newType);
/* 214 */     } else if (newTab == 4) {
/* 215 */       for (Iterator<Integer> iterator = this.cart.iterator(); iterator.hasNext(); ) { int i = ((Integer)iterator.next()).intValue();
/* 216 */         if (newType == 0 || ItemConstants.getInventoryType(i).getType() == newType) {
/* 217 */           this.currentViewingItems.add(Integer.valueOf(i));
         } }

     }
   }

   public void changeCurrentView(List<Integer> items) {
/* 224 */     this.currentViewingItems.clear();
/* 225 */     this.currentViewingItems = items;
   }
 }


/* Location:              C:\PGX\Server.jar!\server\MTSCart.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */